package com.captjack.wechat.admin.configuration.database;

import com.zaxxer.hikari.HikariDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;

/**
 * 手动配置数据源
 *
 * @author Capt Jack
 * @date 2018/5/21
 */
@Configuration
@EnableJpaRepositories(basePackages = {"com.captjack.wechat.admin.repository"})
@EntityScan(basePackages = {"com.captjack.wechat.admin.entity"})
public class MysqlDataSourceConfiguration {

    /**
     * @return 数据源
     */
    @Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.hikari")
    DataSource mysqlDataSource() {
        return new HikariDataSource();
    }

    /**
     * @param dataSource 数据源
     * @return 事务管理器
     */
    @Bean(name = "transactionManager")
    DataSourceTransactionManager mysqlDataSourceTransactionManager(@Qualifier(value = "mysqlDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}
